package com.maxjay.system.util;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import org.apache.commons.io.FileUtils;
import org.apache.log4j.Logger;

import com.maxjay.system.core.cache.SystemDataHolder;

public class FileUploadUtil {
	
	private static Logger logger = Logger.getLogger(FileUploadUtil.class);
	
	/**
	 * 本地文件拷贝
	 * 
	 * @param srcFilePath 源文件路径
	 * @param destFilePath 目标文件路径
	 * @return
	 */
	public static boolean copyLocalFile(String srcRelativePath, String srcFileName, String destRelativePath, String destFileName) {
		try {
			String localUploadDirPath = SystemDataHolder.getInstance().getUploadDirPath();
			File srcFile = new File(localUploadDirPath + srcRelativePath + srcFileName);
			File destFile = new File(localUploadDirPath + destRelativePath + destFileName);
			FileUtils.copyFile(srcFile, destFile);
			return true;
		} catch (IOException e) {
			e.printStackTrace();
			logger.error(e.getMessage(), e);
			throw new RuntimeException(e);
		}
	}
	
	
	/**
	 * 上传到本地服务器
	 * @param inputStream 要上传文件的读入流
	 * @param destRelativePath 上传文件的相对路径
	 * @param destFileName 上传文件的名称
	 * 
	 * @return
	 */
	public static boolean uploadFile2Local(InputStream inputStream, String destRelativePath, String destFileName) {
		try {
			String uploadFilePath = SystemDataHolder.getInstance().getUploadDirPath() + destRelativePath + 
				System.getProperty("file.separator") + destFileName;
			OutputStream out = new BufferedOutputStream(new FileOutputStream(new File(uploadFilePath)));
			byte[] buffer = new byte[1024];
			int length = 0;
			while((length = inputStream.read(buffer))!=-1){
				out.write(buffer, 0, length);
			}
			out.close();
			return true;
		} catch (IOException e) {
			e.printStackTrace();
			logger.error(e.getMessage(), e);
			throw new RuntimeException(e);
		}
	}
	
	/**
	 * 从本地服务器获取文件读入流
	 * 
	 * @param relativePath 文件的相对路径
	 * @param fileName 文件的名称
	 * @return
	 */
	public static InputStream getInputStreamFromLocal(String relativePath, String fileName) {
		try {
			String filePath = SystemDataHolder.getInstance().getUploadDirPath() + relativePath + 
				System.getProperty("file.separator") + fileName;
			InputStream inputStream = new BufferedInputStream(new FileInputStream(new File(filePath)));
			return inputStream;
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			logger.error(e.getMessage(), e);
			throw new RuntimeException(e);
		}
	}
	
	/**
	 * 从本地服务器获取文件输出流
	 * 
	 * @param relativePath 文件的相对路径
	 * @param fileName 文件的名称
	 * @param append 是否输出到文件尾部
	 * @return
	 */
	public static OutputStream getOutputStreamFromLocal(String relativePath, String fileName, boolean append) {
		try {
			String filePath = SystemDataHolder.getInstance().getUploadDirPath()	+ relativePath + 
				System.getProperty("file.separator") + fileName;
			OutputStream outputStream = new BufferedOutputStream(new FileOutputStream(new File(filePath), append));
			return outputStream;
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			logger.error(e.getMessage(), e);
			throw new RuntimeException(e);
		}
	}
	
	/**
	 * 是否存在本地文件
	 * 
	 * @param relativePath 文件的相对路径
	 * @param fileName 文件的名称
	 * @return
	 */
	public static boolean isExistLocalFile(String relativePath, String fileName){
		String filePath = SystemDataHolder.getInstance().getUploadDirPath()	+ relativePath + 
			System.getProperty("file.separator") + fileName;
		File file = new File(filePath);
		return file.exists();
	}
	
}
